package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.insurance.entity.BigInsuranceType;
import com.system.insurance.entity.ContentSetting;
import com.system.insurance.entity.FormFilling;
import com.system.insurance.entity.SmallInsuranceType;
import com.system.insurance.service.ContentSettingService;
import com.system.insurance.service.SmallInsuranceTypeService;
import com.system.insurance.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 小保险类型 前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-10
 */
@Api(tags = "小保险类型")
@RestController
@RequestMapping("/insurance/small-insurance-type")
public class SmallInsuranceTypeController {

    @Autowired
    private SmallInsuranceTypeService smallInsuranceTypeService;

    @Autowired
    private ContentSettingService contentSettingService;

    @ApiOperation(value = "查看所有小保险类")
    @GetMapping("/smallInsuranceTypeList")
    public Result<List<SmallInsuranceType>> smallInsuranceTypeList(){
        List<SmallInsuranceType> list = smallInsuranceTypeService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "查看单个小保险类")
    @GetMapping("/smallInsuranceTypeOne/{id}")
    public Result<SmallInsuranceType> smallInsuranceTypeOne(@PathVariable Integer id){
        SmallInsuranceType  one= smallInsuranceTypeService.getById(id);
        return Result.ok(one);
    }


    @ApiOperation(value = "查看询价单个小保险类表单内容")
    @GetMapping("/contentSettingOne/{directorysId}")
    public Result<ContentSetting> contentSettingOneOne(@PathVariable Integer directorysId){

            LambdaQueryWrapper<SmallInsuranceType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SmallInsuranceType::getDirectorysId,directorysId);
        ContentSetting one= contentSettingService.getById(wrapper);
            return Result.ok(one).message("询价方数据表单内容！");

    }


    @ApiOperation(value = "查看询价单个小保险类表单内容")
    @GetMapping("/contentSettingBjOne")
    public Result<ContentSetting> contentSettingBjOne(@RequestBody FormFilling formFilling){

        Integer smallid = formFilling.getSmallid();
        SmallInsuranceType byId = smallInsuranceTypeService.getById(smallid);


        LambdaQueryWrapper<SmallInsuranceType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmallInsuranceType::getQuotationId,byId.getQuotationId());
        ContentSetting one= contentSettingService.getById(wrapper);
        return Result.ok(one).message("报价方数据表单内容！");

    }

    @ApiOperation(value = "添加小保险种类")
    @PostMapping("/saveSmallInsuranceType/{bigId}")
    public Result<SmallInsuranceType> saveSmallInsuranceType(@PathVariable Integer bigId,@RequestBody SmallInsuranceType smallInsuranceType){
        smallInsuranceType.setCreateTime(LocalDateTime.now());
        smallInsuranceType.setBigid(bigId);
        boolean save = smallInsuranceTypeService.save(smallInsuranceType);

        if (save){
            return Result.ok(smallInsuranceType).message("添加成功");
        }
        return Result.fail(smallInsuranceType).message("添加失败");
    }

    @ApiOperation(value = "修改小保险种类")
    @PostMapping("/updateSmallInsuranceType")
    public Result<SmallInsuranceType> updateSmallInsuranceType(@RequestBody SmallInsuranceType smallInsuranceType){
        smallInsuranceType.setUpdateTime(LocalDateTime.now());
        boolean update = smallInsuranceTypeService.updateById(smallInsuranceType);

        if (update){
            return Result.ok(smallInsuranceType).message("修改成功");
        }
        return Result.fail(smallInsuranceType).message("修改失败");
    }

    @ApiOperation(value = "删除小保险种类")
    @PostMapping("/deleteSmallInsuranceType")
    public Result deleteSmallInsuranceType(@RequestBody SmallInsuranceType smallInsuranceType){
//        删除大保险种类，首先判断是否有下级，有下级概况，优先删除下级，不删除提示不能删除该种类。
        LambdaQueryWrapper<ContentSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ContentSetting::getId,smallInsuranceType.getDirectorysId());
        List<ContentSetting> list = contentSettingService.list(wrapper);

        if (list.size()>0){
            return Result.fail().message("不可删除，请优先删除“表单中详细”数据在进行删除。");
        }else if (list.size()==0){
            boolean remove = smallInsuranceTypeService.removeById(smallInsuranceType.getId());
            if (remove){
                return Result.ok().message("删除成功");
            }
            return Result.fail().message("删除失败");
        }


        boolean remove = smallInsuranceTypeService.removeById(smallInsuranceType.getId());

        if (remove){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<SmallInsuranceType>> index12(@PathVariable Long page, @PathVariable Long limit,@RequestParam(required = false) String name){

        Page<SmallInsuranceType> recommendPage = new Page<>(page,limit);

        if (StringUtils.isEmpty(name)) {

            IPage<SmallInsuranceType> iPage = smallInsuranceTypeService.page(recommendPage);

            return Result.ok(iPage);
        }else {
            LambdaQueryWrapper<SmallInsuranceType> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SmallInsuranceType::getName,name);

            IPage<SmallInsuranceType> iPage = smallInsuranceTypeService.page(recommendPage, wrapper);

            return Result.ok(iPage);
        }
    }


}

